package com.shuoyue.ycgk.ui.estimate;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Estimate;
import com.shuoyue.ycgk.entity.News;
import com.shuoyue.ycgk.entity.NewsType;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface EstimateContract {
    interface View extends BaseView {
        void getEsListSuc(ListWithPage<Estimate> list);

        void getPaperSuc(UserTestPaper userTestPaper);

        void registSuc();
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getEsList(Integer moduleId, int n) {
            apply(model.getEsList(moduleId, n))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<Estimate>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<Estimate>> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.getEsListSuc(listWithPageOptional.getIncludeNull());
                        }
                    });
        }

        public void getPaper(int testPaperId, int evaId) {
            apply(model.getPaper(testPaperId, evaId))
                    .subscribe(new ApiSubscriber<Optional<UserTestPaper>>(mView, this) {
                        @Override
                        public void onNext(Optional<UserTestPaper> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            UserTestPaper u = listWithPageOptional.getIncludeNull();
                            u.setTag(evaId);
                            mView.getPaperSuc(listWithPageOptional.getIncludeNull());

                        }
                    });
        }

        public void registTest(
                int evaId) {
            apply(model.registTest(evaId))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.registSuc();
                        }
                    });
        }
    }

    class Model {
        Observable<BaseResult<ListWithPage<Estimate>>> getEsList(int id, int n) {
            return RetrofitClient.getInstance().getApi().getEsList(id, n);
        }

        Observable<BaseResult<UserTestPaper>> getPaper(Integer testPaperId, int evaId) {
            return RetrofitClient.getInstance().getApi().getEstimatePaper(testPaperId, evaId);
        }


        Observable<BaseResult<String>> registTest(
                Integer evaId) {
            return RetrofitClient.getInstance().getApi().registEsTestMain(evaId);
        }
    }
}
